package com.common.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.account.entity.AuUserLog;
import com.common.account.entity.vo.AuUserLogStatist;
import com.common.account.entity.vo.AuUserLogVo;
import com.common.account.service.IAuUserLogService;
import com.common.basis.result.Result;
import com.common.basis.result.ResultPage;
import com.common.basis.result.ResultSet;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 用户日志表
 *
 * @author mdc
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/account/au-user-log")
@AllArgsConstructor
public class AuUserLogController {


    private final IAuUserLogService iAuUserLogService;

    /**
     * 查询全部
     */
    @GetMapping()
    public ResultPage<AuUserLog> findAll(AuUserLogVo param) {
        LambdaQueryWrapper<AuUserLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(param.getId() != null, AuUserLog::getId, param.getId())
                .orderByDesc(AuUserLog::getCreated);
        IPage<AuUserLog> iPage = iAuUserLogService.page(param.page(), wrapper);
        return ResultSet.page(iPage);
    }

    /**
     * 保存-新增
     */
    @PostMapping()
    public Result<?> save(@RequestBody @Validated AuUserLog param) {
        boolean b = iAuUserLogService.save(param);
        return ResultSet.operation(b);
    }

    /**
     * 保存-修改
     */
    @PutMapping()
    public Result<?> modify(@RequestBody @Validated AuUserLog param) {
        boolean b = iAuUserLogService.updateById(param);
        return ResultSet.operation(b);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public Result<?> del(@RequestParam String id) {
        boolean b = iAuUserLogService.removeById(id);
        return ResultSet.operation(b);
    }

    /**
     * 统计分析-登录日志[企业]
     *
     * @return {@code List<Tenant>}
     */
    @GetMapping("statisticalAnalysis")
    public Result<AuUserLogStatist> statisticalAnalysis() {
        AuUserLogStatist tenantStatist = iAuUserLogService.statisticalAnalysis();
        return ResultSet.go(tenantStatist);
    }

}

